* 1. extract records
** This file takes raw patient record data and extracts all patients with a primary diagnosis of an AMI
** Note: this extraction would be more straight forward in a newly requested dataset; I have datasets that were obtained at different points in time and are therefore structured differently (e.g. epatient ID name changes over time; diagnosis variable name and format changes over time; early years do not have consultant IDs (I applied for these separately and therefore merge them in separately for pre-2010 data)

forval i==1(1)7{

	if `i'==1{
	local finyear "0304"
	local year "2003"
	}
	
	if `i'==2{
	local finyear "0405"
	local year "2004"
	}
	
	if `i'==3{
	local finyear "0506"
	local year "2005"
	}
	
	if `i'==4{
	local finyear "0607"
	local year "2006"
	}

	if `i'==5{
	local finyear "0708"
	local year "2007"
	}
	
	if `i'==6{
	local finyear "0809"
	local year "2008"
	}
	
	if `i'==7{
	local finyear "0910"
	local year "2009"
	}


use "$rawdata/ip_`year'", clear

gen ami = 0
replace ami = 1 if fulldiag2_1=="I21"
replace ami = 1 if fulldiag2_1=="I22"

gen ami_second = 0
replace ami_second = 1 if fulldiag2_1=="I22"

gen stroke = 0
replace stroke = 1 if substr(fulldiag2_1,1,2)=="I6"

keep if ami==1

* A few episodes are near-duplicates; these are purely driven by different info on critical care (not used in the analysis so it doesn't matter which order this is sorted in, you get the same eventual dataset
bys epikey: gen z=_n
keep if z==1

merge 1:1 epikey using "$consids/id_`finyear'.dta",
drop if _merge==2
assert _merge==3

gen finyear=`year'
save "$savedata/ami_`year'", replace
	
}

* 2010-11 onwards
*********************

forval year==2010(1)2017{

use "$rawdata/ip_`year'"	

* Format of diagnosis slightly different in the final year
if `year'==2010 | `year'>2014{
	gen diag3_01 = substr(diag_01,1,3)
}

if `year'==2014{
	rename admidate_mon admidate_month
	rename disdate_mon disdate_month
	gen extract_hesid = encrypted_hesid
}

gen ami = 0
replace ami = 1 if diag3_01=="I21"
replace ami = 1 if diag3_01=="I22"

gen ami_second = 0
replace ami_second = 1 if diag3_01=="I22"

gen stroke = 0
replace stroke = 1 if substr(diag3_01,1,2)=="I6"

keep if ami==1

gen finyear=`year'
save "$savedata/ami_`year'", replace

}

